package com.controller;

import com.context.conexion.EnahoConexion;
import com.model.domain.ubicacion.Distrito;
import com.model.domain.ubicacion.Provincia;
import com.model.service.UbigeoService;
import java.sql.Connection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;

/**
 *
 * @author rdelacruz
 */
public class UbigeoController 
extends AbstractController
{   
    public void provincias(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Connection con = null;    
        try {
            con = EnahoConexion.getConexion();            
            String dpto = request.getParameter("dpto");
            UbigeoService service = (UbigeoService) this.getService();
            List<Provincia> lista =  service.getProvinciaService().findByDepartamento(con, dpto);
            JSONArray jsonA = new JSONArray(lista.toArray());
            response.getWriter().append(jsonA.toString());
        }
        finally {
            if(con!=null) con.close();
        }
        
    }    
    
    public void distritos(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Connection con = null;    
        try {
            con = EnahoConexion.getConexion();            
            String dpto = request.getParameter("dpto");
            String prov = request.getParameter("prov");            
            UbigeoService service = (UbigeoService) this.getService();
            List<Distrito> lista =  service.getDistritoService().findByDepartamentoProvincia(con, dpto,prov);
            JSONArray jsonA = new JSONArray(lista.toArray());
            response.getWriter().append(jsonA.toString());
        }
        finally {
            if(con!=null) con.close();
        }
        
    }
}
